<template>
  <!-- ! 弹框 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogShow"
    width="45%"
    @close="closeDialog"
  >
    <!-- ! 表单 -->
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      label-width="140px"
      :inline="false"
      size="normal"
    >
      <el-form-item label="姓名" prop="username">
        <el-input style="width: 400px" v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input style="width: 400px" v-model="formData.mobile"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input style="width: 400px" v-model="formData.workNumber"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          style="width: 400px"
          v-model="formData.departmentName"
          @focus="getDepartList"
        ></el-input>
        <!-- ? 渲染部门树 -->
        <el-tree
          v-if="treeShow"
          :data="departList"
          :props="{ label: 'name', children: 'child' }"
          @node-click="nodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button size="small" @click="dialogShow = false">取消</el-button>
      <el-button size="small" @click="btnOk" type="primary">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
// 总结核心思路：
// 1. 创建新增、修改组件
// 2. 准备变量   弹框控制变量、表单数据变量、表单验证规则变量
// 3. 绘制弹框(打开关闭逻辑)、绘制表单(验证规则、表单控件的数据绘制)
// 4. 提交逻辑
import EmployeeEnum from "@/api/constant/employees";
import { getDepartmentListApi } from "@/api/department";
import { addEmployeesApi } from "@/api/employees";
import { tranListToTreeData } from "@/utils";
export default {
  name: "addEmployees",
  data() {
    return {
      dialogShow: false,
      formData: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      formRules: {
        username: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 4,
            message: "用户姓名为1-4位",
          },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "change" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
      hireType: EmployeeEnum.hireType,
      departList: [],
      treeShow: false,
    };
  },
  computed: {
    title() {
      return this.formData.id ? "修改" : "新增";
    },
  },
  methods: {
    // 获取部门数据
    async getDepartList() {
      let { depts } = await getDepartmentListApi();
      this.departList = tranListToTreeData(depts, "");
      this.treeShow = true;
    },
    // 树节点被点击
    nodeClick(data) {
      this.formData.departmentName = data.name;
      this.treeShow = false;
    },
    closeDialog() {
      this.formData = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      };
      this.$refs.formRef.resetFields();
      this.treeShow = false;
    },
    // 提交逻辑
    async btnOk() {
      try {
        await this.$refs.formRef.validate();
      } catch (error) {
        return console.log(error);
      }
      await addEmployeesApi(this.formData);
      this.$message.success("新增成功");
      this.$emit("updateList");
      this.dialogShow = false;
    },
  },
};
</script>
<style scoped></style>
